package com.crazy.crazybusinessservice.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazy.crazybusinessservice.mapper.BankCardBalanceMapper;
import com.crazy.crazybusinessservice.service.BankCardBalanceService;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.exception.BusinessException;
import com.crazy.crazymodel.model.entity.BankCardBalance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class BankCardBalanceServiceImpl extends ServiceImpl<BankCardBalanceMapper, BankCardBalance> implements BankCardBalanceService {

    @Override
    public String pay(String cardId, BigDecimal amount) {
        String msg =null;
        QueryWrapper<BankCardBalance> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<BankCardBalance> updateWrapper = new UpdateWrapper<>();
        queryWrapper.eq("bankCardNumber",cardId);
        updateWrapper.eq("bankCardNumber",cardId);
        BankCardBalance balance = this.baseMapper.selectOne(queryWrapper);
        if(balance == null){
            log.info("select bankcardRecord failed");
            msg = "not find cardId";
        }else{
            //如果支付金额比卡内可用余额大则返回余额不足,否则增加冻结余额
            if(!(amount.compareTo(balance.getBalance().subtract(balance.getFreezeBalance()))==1)){
                balance.setFreezeBalance(balance.getFreezeBalance().add(amount));
                this.baseMapper.update(balance,updateWrapper);
                msg = "pay success";
            }else{
                msg = "The balance is insufficient";//余额不足
            }
        }
        return msg;
    }

    @Override
    public String revenue(String cardId, BigDecimal amount) {
        String msg = null;
        QueryWrapper<BankCardBalance> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<BankCardBalance> updateWrapper = new UpdateWrapper<>();
        queryWrapper.eq("bankCardNumber",cardId);
        updateWrapper.eq("bankCardNumber",cardId);
        BankCardBalance balance = this.baseMapper.selectOne(queryWrapper);
        if(balance == null){
            log.info("select bankcardRecord failed");
            msg = "not find cardId";
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"银行卡号不存在");
        }else{
            balance.setBalance(balance.getBalance().add(amount));
            this.baseMapper.update(balance,updateWrapper);
            msg = "revenue success";
        }
        return msg;
    }

    @Override
    public String redeem(String cardId, BigDecimal amount) {
        String msg = null;
        QueryWrapper<BankCardBalance> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<BankCardBalance> updateWrapper = new UpdateWrapper<>();
        queryWrapper.eq("bankCardNumber",cardId);
        updateWrapper.eq("bankCardNumber",cardId);
        BankCardBalance balance = this.baseMapper.selectOne(queryWrapper);
        if(balance == null){
            log.info("select bankcardRecord failed");
            msg = "not find cardId";
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"银行卡号不存在");
        }else{
            if(balance.getFreezeBalance().compareTo(amount) == -1){//银行卡中冻结的金额比申请赎回的金额小?
                log.info("申请赎回金额数据错误");
                msg = "redeem amount error";
            }else{//多线程操作时数据安全性??
                balance.setBalance(balance.getBalance().add(amount));
                this.baseMapper.update(balance,updateWrapper);
                msg = "redeem success";
            }
        }
        return msg;
    }

    @Override
    public String freezeBalanceChancel(String cardId, BigDecimal amount) {
        String msg = null;
        QueryWrapper<BankCardBalance> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<BankCardBalance> updateWrapper = new UpdateWrapper<>();
        queryWrapper.eq("bankCardNumber",cardId);
        updateWrapper.eq("bankCardNumber",cardId);
        BankCardBalance balance = this.baseMapper.selectOne(queryWrapper);
        if(balance == null){
            log.info("select bankcardRecord failed");
            msg = "not find cardId";
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"银行卡号不存在");
        }else{
            if(balance.getFreezeBalance().compareTo(amount) == -1){//银行卡中冻结的金额比申请赎回的金额小?
                log.info("申请撤单金额数据错误");
                msg = "chancel amount error";
            }else{//多线程操作时数据安全性??
                balance.setFreezeBalance(balance.getFreezeBalance().subtract(amount));
                this.baseMapper.update(balance,updateWrapper);
                msg = "redeem success";
            }
        }
        return msg;
    }

    @Override
    public String liquidate(String cardId) {
        String msg =null;
        QueryWrapper<BankCardBalance> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<BankCardBalance> updateWrapper = new UpdateWrapper<>();
        queryWrapper.eq("bankCardNumber",cardId);
        updateWrapper.eq("bankCardNumber",cardId);
        BankCardBalance balance = this.baseMapper.selectOne(queryWrapper);
        if(balance == null){
            log.info("select bankcardRecord failed");
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"银行卡号不存在");
        }else{
            if(balance.getBalance().compareTo(balance.getFreezeBalance() )== -1){//实际金额小于冻结金额
                log.info("冻结金额或实际金额数据错误");
                msg = "The frozen amount or actual amount data is incorrect";
            }else{
                balance.setBalance(balance.getBalance().subtract(balance.getFreezeBalance()));
                balance.setFreezeBalance(BigDecimal.ZERO);
                this.baseMapper.update(balance,updateWrapper);
                msg = "liquidate success";
            }
        }
        return msg;
    }

    @Override
    public String liquidate(String cardId, BigDecimal amount) {
        String msg =null;
        QueryWrapper<BankCardBalance> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<BankCardBalance> updateWrapper = new UpdateWrapper<>();
        queryWrapper.eq("bankCardNumber",cardId);
        updateWrapper.eq("bankCardNumber",cardId);
        BankCardBalance balance = this.baseMapper.selectOne(queryWrapper);
        if(balance == null){
            log.info("select bankcardRecord failed");
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"银行卡号不存在");
        }else{
            if(balance.getBalance().compareTo(amount)== -1 || balance.getFreezeBalance().compareTo(amount) == -1){//实际金额小于冻结金额 或 申请结算金额比实际冻结金额大
                log.info("冻结金额或实际金额数据错误");
                msg = "The frozen amount or actual amount data is incorrect";
            }else{
                balance.setBalance(balance.getBalance().subtract(amount));
                balance.setFreezeBalance(balance.getFreezeBalance().subtract(amount));
                this.baseMapper.update(balance,updateWrapper);
                msg = "liquidate success";
            }
        }
        return msg;
    }



}
